<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\common\controller\ApiUserBaseController;
use api\portal\model\MerchModel;
use model\CityModel;
use model\GoodsModel;
use model\OrderModel;
use plugins\helper\Date;
use think\facade\Db;

class MerchController extends ApiUserBaseController
{

    /**
     * 获取申请状态
     */
    public function info()
    {
        $merchModel = new MerchModel();
        $info = $merchModel->where(['user_id' => $this->userId])->field('name,status,deal_time,reson')->find();
        if ($info) {
            if ($info['status'] == 0) {
                return $this->error('您已提交申请,请勿重复提交!');
            }

            if ($info['status'] == 1) {
                return $this->error('您已通过请勿重复添加!');
            }
        }
        return $this->success('获取成功', $info);
    }

    /**
     * 申请
     */
    public function register()
    {
        $data = $this->request->post();
        $merchModel = new MerchModel();
        $info = $merchModel->where(['user_id' => $this->userId])->find();
        if (!empty($info) && $info['status'] == 1) {
            return $this->error('您已通过申请!');
        }
        $city = CityModel::where(['province' => $data['province'], 'city' => $data['city'], 'area' => $data['area']])->field('id')->find();
        if (!empty($city)) {
            $cityId = $city['id'];
        } else {
            $cityId = Db::name('city')->insertGetId(['province' => $data['province'], 'city' => $data['city'], 'area' => $data['area']]);
        }
        if (empty($order)) {
            return $this->error('订单不存在');
        }

        if (empty($info)) {
            $data['user_id'] = $this->userId;
            $data['city_id'] = $cityId;
            $data['create_time'] = Date::now();
            Db::name('merch')->insert($data);
        } else {
            Db::name('merch')->where([
                'user_id' => $this->userId,
            ])->update($data);
        }
        return $this->success('申请成功');
    }


    /**
     * 保存商品
     */
    public function saveGoods()
    {
        if (empty($this->merchId)) {
            return $this->error('当前商户不可用,请联系管理员');
        }
        $goodsModel = new GoodsModel();
        $data = $this->request->post();

        $data['create_time'] = Date::now();
        $data['merch_id'] = $this->merchId;
        $merchCategory = MerchModel::where(['id' => $data['merch_id']])->field('category')->find();
        $data['category_id'] = $merchCategory['category'];
        $data['images'] = json_encode($data['images']);
        $data['comment'] = htmlspecialchars($data['content']);
        if (isset($data['id']) && $data['id'] > 0) {
            $res = $goodsModel->where(['id' => $data['id']])->update($data);
        } else {
            $res = $goodsModel->insert($data);
        }
        if ($res) {
            return $this->success('上传成功等待审核');
        }

        return $this->error('保存失败!');
    }


    /**
     * 删除
     */
    public function delete()
    {
        $goodsModel = new GoodsModel();
        $id = $this->request->post('id');
        $res = $goodsModel->where(['id' => $id])->delete();
        if ($res) {
            return $this->success('成功');
        }

        return $this->error('保存失败!');
    }

    /**
     * 类目
     */
    public function category()
    {
        $category = Db::name('nav_menu')
            ->alias('nm')
            ->leftJoin('nav n', 'n.id = nm.nav_id')
            ->where('n.is_main', 1)
            ->field('nm.id,nm.name as label,nm.id as value')
            ->select()->toArray();
        return $this->success('成功', $category);
    }


    /**
     * 订单收获
     */
    public function orderReceive()
    {
        $receive_code = $this->request->post('receive_code');
        if (empty($receive_code)) {
            return $this->error('数据错误');
        }

        $order = OrderModel::where(['receive_code' => $receive_code])->find();
        if (empty($order)) {
            return $this->error('订单不存在');
        }

        if ($order['status'] != 1) {
            return $this->error('订单状态不合法');
        }

        if ($this->merchId != $order['merch_id']) {
            return $this->error('账号无法核销');
        }

        $res = Db::name("order")
            ->where(['id' => $order['id']])
            ->update([
                'status' => 2,
                'success_time' => Date::now(),
            ]);
        if ($res) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }


    /**
     * 获取店铺
     */
    public function getMerch()
    {
        $id = $this->request->get('id');
        $merch = MerchModel::where(['id' => $id])->find()->toArray();
        return $this->success('操作成功', $merch);
    }

}
